package com.monkeys.service.impl;

import com.monkeys.Utils.RandomUtil;
import com.monkeys.constants.MaterialConstants;
import com.monkeys.mapper.GoodsDemandMapper;
import com.monkeys.mapper.GoodsMapper;
import com.monkeys.mapper.PoiMapper;
import com.monkeys.pojo.GoodsDemand;
import com.monkeys.service.GoodsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author KuangTeam
 */
@Service
@RequiredArgsConstructor
public class GoodsServiceImpl implements GoodsService {

    private final GoodsMapper goodsMapper;
    private final GoodsDemandMapper goodsDemandMapper;
    private final PoiMapper poiMapper;



    //创建订单(接口)
    @Override
    public GoodsDemand createGoods() {
        Integer ownerId = getOwners().get(0);
        Integer senderId = getSenders().get(0);
        Double weight = RandomUtil.generateRandomNumber2(0,50);              //随机0-50t的货物
        Double volume = RandomUtil.generateRandomNumber2(0,20);              //随机0-20立方米货物
        String typeName = poiMapper.getTypeById(ownerId);

        LocalDate deadTime = LocalDate.now().plusDays(7);      //需求结束时间从现在往后推一星期

        GoodsDemand goodsDemand = GoodsDemand.builder()
                .ownerId(ownerId)
                .senderId(senderId)
                .typeName(typeName)
                .weight(weight)
                .volume(volume)
                .deadTime(deadTime)
                .build();

        //向货物需求表插入数据
        goodsDemandMapper.insert(goodsDemand);

        return goodsDemand;
    }


    //创建n个订单
    @Override
    public List<GoodsDemand> createGoodsById(Long n) {
        return createGoodDemands(n);
    }


    //更新订单
    @Override
    public void updateGoodsDemand(GoodsDemand goodsDemand) {
        goodsDemandMapper.update(goodsDemand);
    }


    //删除所有订单
    @Override
    public void deleteAll() {
        goodsDemandMapper.deleteAll();
    }


    //获取所有订单信息
    @Override
    public List<GoodsDemand> getAllGoodsDemands() {
        return goodsDemandMapper.selectAllGoodsDemands();
    }


    //随机返回所有的货主的id
    @Override
    public List<Integer> getOwners() {
        List<Integer> allList = goodsMapper.getOwners();    //所有的非工厂id
        // 打乱集合的元素顺序
        Collections.shuffle(allList);
        return allList;
    }


    //随机返回所有的工厂(发送方)的id
    @Override
    public List<Integer> getSenders() {
        List<Integer> allList = goodsMapper.getSenders();
        // 打乱集合的元素顺序
        Collections.shuffle(allList);
        return allList;
    }


    //返回所有货物的ids
    @Override
    public List<Integer> getGoods() {
        List<Integer> allList = goodsMapper.getGoods();
        // 打乱集合的元素顺序
        Collections.shuffle(allList);
        return allList;
    }


    //创建n个订单（方法）
    public List<GoodsDemand> createGoodDemands(Long n){
        List<GoodsDemand> result = new ArrayList<>();

        //为了保证体现出至少一条送货链条，这里规定订单数要大于等于三
        //为了保证生成的随机订单必须要有链条所有信息，这里规定前四个包含原木-木材-板材-家具，后面则可以随机
        for(int i = 1; i <= n; i++){
            Integer ownerId;
            Integer senderId;
            if(i == 1){
                 ownerId = poiMapper.getIdByMaterial(MaterialConstants.LOG_OF_WOOD);  //原木
                 senderId = poiMapper.getIdByMaterial(MaterialConstants.WOOD);        //木材
            } else if (i == 2){
                ownerId = poiMapper.getIdByMaterial(MaterialConstants.WOOD);          //木材
                senderId = poiMapper.getIdByMaterial(MaterialConstants.SHEET_METAL);  //板材
            }else if(i == 3){
                ownerId = poiMapper.getIdByMaterial(MaterialConstants.SHEET_METAL);   //板材
                senderId = poiMapper.getIdByMaterial(MaterialConstants.FURNITURE);    //家具
            }else {
                //剩余的随机选取即可
                ownerId = getOwners().get(0);
                senderId = getSenders().get(0);
            }

            Double weight = RandomUtil.generateRandomNumber2(0,50);              //随机0-50t的货物
            Double volume = RandomUtil.generateRandomNumber2(0,20);              //随机0-20立方米货物
            String typeName = poiMapper.getTypeById(ownerId);
            LocalDate deadTime = LocalDate.now().plusDays(7);      //需求结束时间从现在往后推一星期

            GoodsDemand goodsDemand = GoodsDemand.builder()
                    .ownerId(ownerId)
                    .senderId(senderId)
                    .typeName(typeName)
                    .weight(weight)
                    .volume(volume)
                    .deadTime(deadTime)
                    .build();

            //向货物需求表插入数据
            goodsDemandMapper.insert(goodsDemand);
            //插入结果
            result.add(goodsDemand);
        }
        return result;
    }
}
